// facility/mapLamp/mapLamp.js
import http from '../../http'
Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    infoType: '',
    nowProjectInfo: '',
    nowfacilityInfo: ''
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 灯
    bts: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 开关灯
    switch (event) {
			wx.showLoading({
				title: '加载中...'
			});
      console.log(event.currentTarget.dataset);
      let data = event.currentTarget.dataset
      // 区域开关灯
      if (this.properties.infoType == 'project') {
        let params = {
          command: "LAMP_CONTROL",
          brightness: 0,
          projectId: this.properties.nowProjectInfo.id
        }
        let issue = () => {
          http.post({
            url: '/zg/command/group',
            data: params,
            then: (res) => {
              console.log('区域开关灯', res);
              if (res.data.code == 0) {
								wx.showToast({
									title: '下发成功',
									icon: 'success',
								})
								this.update()
              }
            }
          })
        }
        if (data.order == 'open') {
          this.setData({
            bts: 100
          }, () => {
            params.brightness = this.data.bts
            issue()
          })
        } else {
          this.setData({
            bts: 0
          }, () => {
            params.brightness = this.data.bts
            issue()
          })
        }
      } else {
        // 设备开关灯
        let params = {
          command: "LAMP_CONTROL",
          brightness: 0,
          deviceId: this.properties.nowfacilityInfo.id
        }
        let issue = () => {
          http.post({
            url: '/zg/command/one',
            data: params,
            then: (res) => {
              console.log('设备开关灯', res);
              if (res.data.code == 0) {
								wx.showToast({
									title: '下发成功',
									icon: 'success',
								})
								this.update()
              }
            }
          })
        }
        if (data.order == 'open') {
          this.setData({
            bts: 100
          }, () => {
            params.brightness = this.data.bts
            issue()
          })
        } else {
          this.setData({
            bts: 0
          }, () => {
            params.brightness = this.data.bts
            issue()
          })
        }
      }

    },
    // 亮度
    btsControl() {
			wx.showLoading({
				title: '加载中...'
			});
      let url = ''
      let params = ''
      // 区域亮度
      if (this.properties.infoType == 'project') {
        url = '/zg/command/group'
        params = {
          command: "LAMP_CONTROL",
          brightness: this.data.bts,
          projectId: this.properties.nowProjectInfo.id
        }
      } else {
        // 设备亮度
        url = '/zg/command/one'
        params = {
          command: "LAMP_CONTROL",
          brightness: this.data.bts,
          deviceId: this.properties.nowfacilityInfo.id
        }
      }
      http.post({
        url: url,
        data: params,
        then: (res) => {
          console.log('区域(设备)调节亮度', res);
          if (res.data.code == 0) {
						wx.showToast({
							title: '下发成功',
							icon: 'success',
						})
						this.update()
          }
        }
      })
    },
    btsChange(event) {
      this.setData({
        bts: event.detail
      }, () => {
        this.btsControl()
      })
    },
    btsChange1(event) {
      this.setData({
        bts: event.detail.value
      })
		},
		update(){
			this.triggerEvent('update')
		}
  }
})